﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;

namespace CokeZeroCodeUITest
{
    public class Xml2Html
    {
        /// <summary>
        /// Transform xml file into html string
        /// </summary>
        /// <param name="xslt">XSLT file path</param>
        /// <param name="xml">Source xml file path</param>
        /// <returns>HTML string</returns>
        public static string Transform(string xslt, string xml)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xslt);
            XPathDocument xDoc = new XPathDocument(xml);
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlTextWriter.Create(sb, null);
            transform.Transform(xDoc, writer);
            return sb.ToString();
        }

        /// <summary>
        /// Transform xml file into html file
        /// </summary>
        /// <param name="xslt">XSLT file path</param>
        /// <param name="xml">Source xml file path</param>
        /// <param name="output">Output html file path</param>
        public static void Transform(string xslt, string xml, string output)
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xslt);

            XPathDocument xDoc = new XPathDocument(xml);

            using (XmlTextWriter writer = new XmlTextWriter(output, null))
            {
                transform.Transform(xDoc, null, writer, new XmlUrlResolver());
            }
        }
    }
}
